<?

class Smutty_Session {

	var $user = null;

	/**
	 *  constructor
	 *
	 */

	function Smutty_Session() {
		Smutty_Main::loadClass( 'Smutty_Session_User' );
		session_start();
		foreach ( $_SESSION as $name => $value ) {
			//echo "LOAD $name = $value";
			$this->$name = $value;
		}
		if ( !$this->user )
			$this->tryAuth();
	}

	/**
	 *  returns the instance of the session class
	 *
	 *  @return Smutty_Session
	 *
	 */

	function &getInstance() {
		static $instance;
		if ( $instance == null )
			$instance = new Smutty_Session();
		return $instance;
	}

	/**
	 *  tries to authenticate a user.  if the authentication succeeds
	 *  then the $user property will be set accordingly.
	 *
	 */

	function tryAuth() {

		Smutty_Main::loadClass(
			'Smutty_Session_Abstract', 'Smutty_Session_Standard'
		);

		$cfg = Smutty_Config::getInstance();

		switch ( $cfg->get('auth.type') ) {
			case 'standard':
				$this->user = Smutty_Session_Standard::getUser();
				break;
		}

		$_SESSION['user'] = $this->user;

	}

	/**
	 *  this function saves any changes that have been
	 *  made to the session object
	 *
	 */

	function save() {
		foreach ( $this as $name => $value )
			$_SESSION[$name] = $value;
	}

}

?>